Skip to main content

Custom Integration - SharePoint Functions

GetSharePointFiles

// Async function to retrieve and sort files from SharePoint
async function GetSharePointFiles(payload) {
try {
// SharePoint API endpoint for fetching files
const queryurl = "{{ GetSharePointFilesUrl }}";

// Perform a POST request to the SharePoint API
const response = await fetch(queryurl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and sort the response data
const data = await response.json();
data.sort((a, b) => {
// Compare FileSystemObjectType in descending order
if (a.FileSystemObjectType > b.FileSystemObjectType) return -1;
if (a.FileSystemObjectType < b.FileSystemObjectType) return 1;

// If FileSystemObjectType is equal, compare FileLeafRef in ascending order
return a.FileLeafRef.localeCompare(b.FileLeafRef);
});

// Return the sorted data
return data;
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during fetching files:', error);
throw error;
}
}

GrantSharePointCollaborationAccess

async function GrantSharePointCollaborationAccess(payload) {
try {
// SharePoint API endpoint for file upload
const grantCollabUrl = "{{ GrantCollaborationUrl }}";

// Perform a POST request to the SharePoint API with FormData
const response = await fetch(grantCollabUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and return the response data as JSON
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during granting collaboration:', error);
throw error;
}
}

NotifyTeamOfFileUpload

async function NotifyTeamOfFileUpload(payload) {
try {
// SharePoint API endpoint for file upload
const queryUrl = "{{ emailTeamsUrl }}";

// Perform a POST request to the SharePoint API with FormData
const response = await fetch(queryUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and return the response data as JSON
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error sending notification to teams:', error);
throw error;
}
}

GetSharePointFileEditors

// Async function to retrieve file editors information from SharePoint
async function GetSharePointFileEditors(payload) {
try {
// SharePoint API endpoint for fetching file editors information
const queryurl = "{{ GetSharePointFilesEditorUrl }}";

// Perform a POST request to the SharePoint API
const response = await fetch(queryurl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and return the response data as JSON
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during fetching file editors:', error);
throw error;
}
}

UploadFileToSharePoint

// Async function to upload a file to SharePoint
async function UploadFileToSharePoint(formData) {
try {
// SharePoint API endpoint for file upload
const uploadUrl = "{{ UploadSharePointFileUrl }}";

// Perform a POST request to the SharePoint API with FormData
const response = await fetch(uploadUrl, {
method: 'POST',
body: formData,
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Resolve the promise with a success message
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during file upload:', error);
throw error;
}
}

CreateFolderInSharePoint

// Async function to create a folder in SharePoint
async function CreateFolderInSharePoint(payload) {
try {
// SharePoint API endpoint for folder creation
const createUrl = "{{ CreateSharePointFolderUrl }}";

// Perform a POST request to the SharePoint API
const response = await fetch(createUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and return the response data as JSON
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during folder creation:', error);
throw error;
}
}

DeleteFileFromSharePoint

// Async function to delete a file from SharePoint
async function DeleteFileFromSharePoint(payload) {
try {
// SharePoint API endpoint for file deletion
const deletionUrl = "{{ DeleteSharePointFileUrl }}";

// Perform a POST request to the SharePoint API
const response = await fetch(deletionUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Resolve the promise with a success message
return "File Removed From SharePoint";
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during file deletion:', error);
throw error;
}
}

DownloadFileFromSharePoint

// Async function to download a file from SharePoint
async function DownloadFileFromSharePoint(payload) {
try {
// SharePoint API endpoint for file download
const downloadUrl = '{{ DownloadSharePointFileUrl }}';

// Perform a POST request to the SharePoint API
const response = await fetch(downloadUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and return the response data as JSON
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during file download:', error);
throw error;
}
}

ExtractDataFromSharePointFile

async function ExtractDataFromSharePointFile(sectionName, payload) {
try {
console.log("Extracting Data from File");
// SharePoint API endpoint for file download
const EmploymentsURL = '{{ AI_UK_TR_Employments }}';
const PriorYearK1 = '{{ AI_Funds_TIR_PriorYearK1 }}';


switch(sectionName) {
case "Employments":
var extractionUrl = EmploymentsURL;
break;
case "Prior Year Form 1065 and Schedule K-1s":
var extractionUrl = PriorYearK1;

break;
default:
var extractionUrl = '';
break;
}

console.log(`extractionUrl: ${extractionUrl}`);

// Perform a POST request to the SharePoint API
const response = await fetch(extractionUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
});

// Ensure the response is successful before proceeding
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

// Wait for a short period before resolving the promise (if necessary)
await new Promise(resolve => setTimeout(resolve, 10));

// Parse and return the response data as JSON
return await response.json();
} catch (error) {
// Log and re-throw the error for handling elsewhere if needed
console.error('Error during data extraction from file:', error);
throw error;
}
}